home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 5 / Apprentice-Release5.iso / Environments / PowerMacOberon feb96 / Source / TextFrames.Mod (.txt) < prev    next >
Oberon Text  |  1996-01-31  |  60KB  |  1,274 lines

  1. Syntax10.Scn.Fnt
  2. Syntax10i.Scn.Fnt
  3. Syntax10b.Scn.Fnt
  4. Syntax8.Scn.Fnt
  5. Syntax12.Scn.Fnt
  6. Arial12.Scn.Fnt
  7. MODULE TextFrames;    (** CAS/MH/HM 12.10.1993 / mf 12.10.93 / mah 26.7.94 **)
  8.     IMPORT Modules, Input, Display, Fonts, Viewers, Oberon, MenuViewers, Texts, Files, Macintosh;
  9.     CONST
  10.         (** update message IDs **)
  11.             replace* = 0; insert* = 1; delete* = 2;
  12.         (** units **)
  13.             mm* = 36000;  Unit* = 10000;
  14.         (** parc options **)
  15.             gridAdj* = 0;  leftAdj* = 1;  rightAdj* = 2;  pageBreak* = 3;    twoColumns* = 4;
  16.         (** maximum number of TAB stops in Parc **)
  17.             MaxTabs* = 32;
  18.         AdjMask = {leftAdj, rightAdj};
  19.         TAB = 9X; CR = 0DX; DEL = 7FX; CRSL = 0C4X; CRSR = 0C3X; LF = 0A4X; BRK = 0B9X; ShiftBRK = 0B8X;
  20.         CRSU = 0C1X; CRSD = 0C2X; DELRIGHT = 008X;        (*<< mah *) 
  21.         EOL = 093X; HOME = 091X; PGUP = 0ACX; PGDN = 0ADX; (*<< mah *) 
  22.         AdjustSpan = 30; MinTabWidth = 1 * mm; StdTabWidth = 4 * mm;
  23.         rightKey = 0; middleKey = 1; leftKey = 2; cancel = {rightKey, middleKey, leftKey};
  24.     TYPE
  25.         Parc* = POINTER TO ParcDesc;
  26.         ParcDesc* = RECORD (Texts.ElemDesc)
  27.             left*: LONGINT;    (** distance from (F.X + F.left); in units **)
  28.             first*: LONGINT;    (** first line indentation from P.left; in units **)
  29.             width*: LONGINT;    (** parc width; in units **)
  30.             lead*: LONGINT;    (** distance to previous line; in units **)
  31.             lsp*: LONGINT;    (** line spacing of text after P; in units **)
  32.             dsr*: LONGINT;    (** descender of text after P; in units **)
  33.             opts*: SET;
  34.             nofTabs*: INTEGER;
  35.             tab*: ARRAY MaxTabs OF LONGINT    (** in units **)
  36.         END;
  37.         TextLine = POINTER TO TextLineDesc;
  38.         Location* = RECORD
  39.             org*, pos*: LONGINT;
  40.             x*, y*, dx*, dy*: INTEGER;
  41.             line: TextLine
  42.         END;
  43.         TextLineDesc = RECORD
  44.             next: TextLine;
  45.             eot: BOOLEAN;                (* contains end of text *)    
  46.             indent: LONGINT;                (* line indentation in units *)
  47.             w, h, dsr: INTEGER;            (* bounding box clipped to frame (w including indent) *)
  48.             w0, nob: INTEGER;            (* unclipped width (including indent), number of contained blanks: nob > 0 if text line wraps around *)
  49.             org, len, span: LONGINT;    (* len ... characters w/o; span ... w/ trailing CR or white space, if any *)
  50.             P: Parc;                            (* last parc before this text line *)
  51.             pbeg: LONGINT                (* position of P *)
  52.         END;
  53.         Frame* = POINTER TO FrameDesc;
  54.         FrameDesc* = RECORD (Display.FrameDesc)
  55.             text*: Texts.Text;
  56.             org*: LONGINT;
  57.             col*, left*, right*, top*, bot*: INTEGER;
  58.             markH*: INTEGER;    (** position of tick mark in scroll bar (< 0 => no tick mark) **)
  59.             barW*: INTEGER;    (** scroll bar width **)
  60.             time*: LONGINT;    (** selection time **)
  61.             hasCar*, hasSel*, showsParcs*: BOOLEAN;    (** caret/selection present; parcs visible **)
  62.             carloc*, selbeg*, selend*: Location;
  63.             focus*: Display.Frame;    (** frame of nested element if this element contains the focus **)
  64.             trailer: TextLine    (* ring with trailer and header *)
  65.         END;
  66.         DisplayMsg* = RECORD (Texts.ElemMsg)
  67.             prepare*: BOOLEAN;
  68.             fnt*: Fonts.Font;
  69.             col*: SHORTINT;
  70.             pos*: LONGINT;    (** position in host text **)
  71.             frame*: Display.Frame;    (** ~prepare => host frame **)
  72.             X0*, Y0*: INTEGER;    (** ~prepare => receiver origin in screen space **)
  73.             indent*: LONGINT;    (** prepare => width already consumed in line, in units **)
  74.             elemFrame*: Display.Frame    (** optional return parameter **)
  75.         END;
  76.         TrackMsg* = RECORD (Texts.ElemMsg)
  77.             X*, Y*: INTEGER;
  78.             keys*: SET;
  79.             fnt*: Fonts.Font;
  80.             col*: SHORTINT;
  81.             pos*: LONGINT;    (** position in host text **)
  82.             frame*: Display.Frame;    (** host frame **)
  83.             X0*, Y0*: INTEGER    (** receiver origin in screen space **)
  84.         END;
  85.         FocusMsg* = RECORD (Texts.ElemMsg)
  86.             focus*: BOOLEAN;    (** whether to focus or to defocus **)
  87.             elemFrame*: Display.Frame;    (** focus/defocus target **)
  88.             frame*: Display.Frame    (** host frame **)
  89.         END;
  90.         NotifyMsg* = RECORD (Display.FrameMsg)
  91.             frame*: Display.Frame    (** host frame **)
  92.         END;
  93.         UpdateMsg* = RECORD (Display.FrameMsg)
  94.             id*: INTEGER;
  95.             text*: Texts.Text;
  96.             beg*, end*: LONGINT
  97.         END;
  98.         InsertElemMsg* = RECORD (Display.FrameMsg)
  99.             e*: Texts.Elem
  100.         END;
  101.         SelectMsg = RECORD (Display.FrameMsg)
  102.             text: Texts.Text;
  103.             beg, end: LONGINT;
  104.             time: LONGINT
  105.         END;
  106.         menuH*, barW*, left*, right*, top*, bot*: INTEGER;
  107.         defParc*: Parc;
  108.         (*shared globals => get rid off in a later version?*)
  109.         W, W0: Texts.Writer;
  110.         B: Texts.Buffer;
  111.         P: Parc;
  112.         pbeg: LONGINT;    (*inv T[pbeg] = P*)
  113.         R: Texts.Reader;
  114.         nextCh: CHAR;    (*inv Base(R) = T => T[Pos(R)-1] = nextCh]*)
  115.         par: Oberon.ParList;
  116.         neutralize: Oberon.ControlMsg;
  117.     PROCEDURE Min (x, y: INTEGER): INTEGER;
  118.     BEGIN IF x < y THEN RETURN x ELSE RETURN y END
  119.     END Min;
  120.     PROCEDURE Max (x, y: INTEGER): INTEGER;
  121.     BEGIN IF x > y THEN RETURN x ELSE RETURN y END
  122.     END Max;
  123.     PROCEDURE MarkMenu (F: Frame);
  124.         VAR R: Texts.Reader; V: Viewers.Viewer; T: Texts.Text; ch: CHAR;
  125.     BEGIN V := Viewers.This(F.X, F.Y);
  126.         IF (V IS MenuViewers.Viewer) & (V.dsc IS Frame) & (F # V.dsc) THEN
  127.             T := V.dsc(Frame).text;
  128.             IF T.len > 0 THEN Texts.OpenReader(R, T, T.len - 1); Texts.Read(R, ch) ELSE ch := 0X END;
  129.             IF ch # "!" THEN Texts.Write(W0, "!"); Texts.Append(T, W0.buf) END
  130.         END
  131.     END MarkMenu;
  132.     (* Element Subframes *)
  133.     PROCEDURE InvertBorder (F: Display.Frame);
  134.     BEGIN
  135.         Display.ReplPattern(Display.white, Display.grey1, F.X-1, F.Y-1, F.W+2, 1, Display.invert);
  136.         Display.ReplPattern(Display.white, Display.grey1, F.X-1, F.Y+F.H, F.W+2, 1, Display.invert);
  137.         Display.ReplPattern(Display.white, Display.grey1, F.X-1, F.Y, 1, F.H, Display.invert);
  138.         Display.ReplPattern(Display.white, Display.grey1, F.X+F.W, F.Y, 1, F.H, Display.invert)
  139.     END InvertBorder;
  140.     PROCEDURE InvalSubFrames (F: Frame; x, y, w, h: INTEGER);    (* removes and suspends all subframes partly in (x, y, w, h) *)
  141.         VAR p, f: Display.Frame; msg: MenuViewers.ModifyMsg;
  142.     BEGIN
  143.         IF (w > 0) & (h > 0) THEN f := F.dsc;
  144.             IF f # NIL THEN p := f; f := p.next END;
  145.             WHILE f # NIL DO
  146.                 IF (f.X < x + w) & (f.X + f.W > x) & (f.Y < y + h) & (f.Y + f.H > y) THEN p.next := f.next;
  147.                     msg.id := MenuViewers.reduce; msg.dY := 0; msg.Y := f.Y; msg.H := 0;
  148.                     f.handle(f, msg)
  149.                 ELSE p := f
  150.                 END;
  151.                 f := p.next
  152.             END;
  153.             f := F.dsc;
  154.             IF (f # NIL) & (f.X < x + w) & (f.X + f.W > x) & (f.Y < y + h) & (f.Y + f.H > y) THEN F.dsc := F.dsc.next;
  155.                 msg.id := MenuViewers.reduce; msg.dY := 0; msg.Y := f.Y; msg.H := 0;
  156.                 f.handle(f, msg)
  157.             END
  158.         END
  159.     END InvalSubFrames;
  160.     PROCEDURE ShiftSubFrames (F: Frame; oldY, newY, h: INTEGER);    (* shift (F.X, oldY, F.W, h) to (F.X, newY, F.W, h) *)
  161.         VAR f: Display.Frame; msg: MenuViewers.ModifyMsg;
  162.     BEGIN
  163.         IF oldY > newY THEN InvalSubFrames(F, F.X, newY, F.W, oldY - newY)
  164.         ELSE InvalSubFrames(F, F.X, oldY + h, F.W, newY - oldY)
  165.         END;
  166.         f := F.dsc;
  167.         WHILE f # NIL DO
  168.             IF (f.Y < oldY + h) & (f.Y + f.H > oldY) THEN INC(f.Y, newY - oldY);
  169.                 msg.id := MenuViewers.reduce; msg.dY := 0; msg.Y := f.Y; msg.H := f.H;
  170.                 f.handle(f, msg)
  171.             END;
  172.             f := f.next
  173.         END
  174.     END ShiftSubFrames;
  175.     PROCEDURE NotifySubFrames (F: Frame; VAR msg: Display.FrameMsg);
  176.         VAR p, f: Display.Frame;
  177.     BEGIN f := F.dsc;
  178.         IF msg IS NotifyMsg THEN msg(NotifyMsg).frame := F END;
  179.         WHILE f # NIL DO p := f; f := f.next; p.handle(p, msg) END
  180.     END NotifySubFrames;
  181.     (* Display Primitives *)
  182.     PROCEDURE DrawCursor (x, y: INTEGER);
  183.     BEGIN Oberon.DrawCursor(Oberon.Mouse, Oberon.Arrow, x, y)
  184.     END DrawCursor;
  185.     PROCEDURE TrackMouse (VAR x, y: INTEGER; VAR keys, keysum: SET);
  186.     BEGIN Input.Mouse(keys, x, y); keysum := keysum + keys; DrawCursor(x, y)
  187.     END TrackMouse;
  188.     PROCEDURE EraseRect (F: Frame; x, y, w, h: INTEGER);
  189.     BEGIN Display.ReplConst(F.col, x, y, w, h, Display.replace); InvalSubFrames(F, x, y, w, h)
  190.     END EraseRect;
  191.     PROCEDURE Erase (F: Frame; x, y, w, h: INTEGER);    (*RemoveMarks optimization*)
  192.     BEGIN
  193.         IF h > 0 THEN Oberon.RemoveMarks(x, y, w, h); EraseRect(F, x, y, w, h) END
  194.     END Erase;
  195.     PROCEDURE Shift (F: Frame; oldY, newY, h: INTEGER);    (*RemoveMarks optimization*)
  196.     BEGIN
  197.         IF (oldY # newY) & (h > 0) THEN
  198.             Oberon.RemoveMarks(F.X + F.left, Min(oldY, newY), F.W - F.left, Max(oldY, newY) + h);
  199.             Display.CopyBlock(F.X + F.left, oldY, F.W - F.left, h, F.X + F.left, newY, Display.replace);
  200.             ShiftSubFrames(F, oldY, newY, h)
  201.         END
  202.     END Shift;
  203.     PROCEDURE InvertCaret (F: Frame);
  204.         VAR loc: Location; bot: INTEGER;
  205.     BEGIN loc := F.carloc; bot := loc.y + loc.line.dsr - 6;
  206.         Display.CopyPatternC(F, Display.white, Display.hook, loc.x, bot, Display.invert)
  207.     END InvertCaret;
  208.     PROCEDURE InvertRect (F: Frame; x, y, w, h: INTEGER);    (*clips to right and bottom frame margin*)
  209.     BEGIN
  210.         IF x + w > F.X + F.W - F.right THEN w := F.X + F.W - F.right - x END;
  211.         IF y >= F.Y + F.bot THEN Display.ReplConst(Display.white, x, y, w, h, Display.invert) END
  212.     END InvertRect;
  213.     PROCEDURE InvertSelection (F: Frame; beg, end: Location);
  214.         VAR t: TextLine; ex, rx, w, py: INTEGER;
  215.     BEGIN
  216.         rx := F.X + F.W - F.right; t := end.line;
  217.         IF t.eot OR (end.pos <= t.org + t.len) THEN ex := end.x ELSE ex := rx END;
  218.         IF beg.line = end.line THEN InvertRect(F, beg.x, beg.y, ex - beg.x, beg.line.h)
  219.         ELSE t := beg.line; py := beg.y; w := F.W - F.left - F.right;
  220.             InvertRect(F, beg.x, py, rx - beg.x, t.h); t := t.next; DEC(py, t.h);
  221.             WHILE t # end.line DO InvertRect(F, F.X + F.left, py, w, t.h); t := t.next; DEC(py, t.h) END;
  222.             InvertRect(F, F.X + F.left, py, ex - (F.X + F.left), t.h)
  223.         END
  224.     END InvertSelection;
  225.     PROCEDURE CoordToPos (F: Frame; mh: INTEGER): LONGINT;
  226.         VAR h: INTEGER;
  227.     BEGIN h := F.H - 1;
  228.         IF h > 0 THEN RETURN (h - mh) * F.text.len DIV h ELSE RETURN 0 END
  229.     END CoordToPos;
  230.     PROCEDURE ShowBar (F: Frame; botH, topH: INTEGER);
  231.     BEGIN
  232.         IF (F.left > F.barW) & (F.barW > 0) THEN
  233.             Display.ReplConst(Display.white, F.X + F.barW - 1, F.Y + botH, 1, topH - botH, Display.replace)
  234.         END
  235.     END ShowBar;
  236.     PROCEDURE Tick (F: Frame);
  237.     BEGIN
  238.         IF (0 <= F.markH) & (F.markH < F.H) & (F.left > F.barW) & (F.barW > 6) & (F.H > 2) THEN
  239.             Display.ReplConst(Display.white, F.X + 1, F.Y + F.markH, F.barW - 6, 2, Display.invert)
  240.         END
  241.     END Tick;
  242.     PROCEDURE ShowTick (F: Frame);    (* removes global marks as needed *)
  243.         VAR h, mh: INTEGER; len: LONGINT;
  244.     BEGIN
  245.         h := F.H - 2; len := F.text.len;
  246.         IF len > 0 THEN mh := SHORT(h - h * F.org DIV len) ELSE mh := h END;
  247.         IF F.markH # mh THEN Oberon.RemoveMarks(F.X, F.Y, F.barW, F.H);
  248.             Tick(F); F.markH := mh; Tick(F)
  249.         END
  250.     END ShowTick;
  251.     PROCEDURE Mark* (F: Frame; mark: INTEGER);
  252.     BEGIN
  253.         Erase(F, F.X, F.Y, F.barW - 1, F.H); F.markH := -1;
  254.         IF (mark < 0) & (F.H >= 16) THEN
  255.             Display.CopyPattern(Display.white, Display.downArrow, F.X, F.Y, Display.invert)
  256.         ELSIF mark > 0 THEN
  257.             ShowTick(F)
  258.         END
  259.     END Mark;
  260.     (** Parcs **)
  261.     PROCEDURE ParcBefore* (T: Texts.Text; pos: LONGINT; VAR P: Parc; VAR beg: LONGINT);
  262.         VAR R: Texts.Reader;
  263.     BEGIN Texts.OpenReader(R, T, pos + 1);
  264.         REPEAT Texts.ReadPrevElem(R) UNTIL R.eot OR (R.elem IS Parc);
  265.         IF R.eot THEN P := defParc; beg := -1 ELSE P := R.elem(Parc); beg := Texts.Pos(R) END
  266.     END ParcBefore;
  267.     PROCEDURE InitDefParc;
  268.     BEGIN
  269.         IF Modules.ThisMod("ParcElems") = NIL THEN HALT(99) END
  270.         (* side effect: body of ParcElems initialises defParc *)
  271.     END InitDefParc;
  272.     (* Screen Metrics *)
  273.     PROCEDURE Tab (dw: INTEGER; VAR dx: INTEGER);    (*P set*)
  274.         (* dw = line width from left margin to caret (in pixels); dx = distance from caret to next tab stop (in pixels) *)
  275.         VAR i, n: INTEGER; w: LONGINT;
  276.     BEGIN
  277.         i := 0; n := P.nofTabs; w := LONG(dw) * Unit + MinTabWidth;
  278.         IF dw < 0 THEN dx := -dw
  279.         ELSE
  280.             WHILE (i < n) & (P.tab[i] < w) DO INC(i) END;
  281.             IF i < n THEN dx := SHORT((P.tab[i] - LONG(dw) * Unit) DIV Unit)
  282.             ELSE dx := StdTabWidth DIV Unit
  283.             END
  284.         END
  285.     END Tab;
  286.     PROCEDURE MeasureSpecial (dw: INTEGER; VAR dx, x, y, w, h: INTEGER);
  287.         (* returns metrics of nextCh (nextCh <= " "); sends prepare message to elements; P, R, nextCh set *)
  288.         VAR e: Texts.Elem; pat: Display.Pattern; msg: DisplayMsg;
  289.     BEGIN
  290.         IF nextCh = " " THEN Display.GetChar(R.fnt.raster, nextCh, dx, x, y, w, h, pat);
  291.             x := 0; y := 0; w := dx; h := 0
  292.         ELSIF nextCh = TAB THEN Tab(dw, dx); x := 0; y := 0; w := dx; h := 0
  293.         ELSIF R.elem # NIL THEN e := R.elem;
  294.             msg.prepare := TRUE; msg.indent := LONG(dw) * Unit;
  295.             msg.fnt := R.fnt; msg.col := R.col; msg.pos := Texts.Pos(R)-1;
  296.             msg.Y0 := -SHORT(P.dsr DIV Unit);    (*<<< 18-Nov-91*)
  297.             e.handle(e, msg);
  298.             w := SHORT(e.W DIV Unit);
  299.             dx := w; x := 0; y := msg.Y0; h := SHORT(e.H DIV Unit)    (*<<< 18-Nov-91*)
  300.         ELSE Display.GetChar(R.fnt.raster, nextCh, dx, x, y, w, h, pat)
  301.         END
  302.     END MeasureSpecial;
  303.     PROCEDURE GetSpecial (F: Frame; VAR n: INTEGER; cn, ddx, dw: INTEGER; VAR dx, x, y, w, h: INTEGER);
  304.         (* returns metrics of nextCh (nextCh <= " "); no prepare message to elements; extends blanks for block adjust *)
  305.         (* cn ... add 1 pixel to first cn blanks (block adjust); ddx ... add ddx pixels to every blank (block adjust) *)
  306.         (*P, R, nextCh set*)
  307.         VAR e: Texts.Elem; pat: Display.Pattern;
  308.     BEGIN
  309.         IF nextCh = " " THEN Display.GetChar(R.fnt.raster, nextCh, dx, x, y, w, h, pat);
  310.             x := 0; y := 0; INC(dx, ddx); INC(n); IF n <= cn THEN INC(dx) END;    (*space correction for block adjustment*)
  311.             w := dx; h := 0
  312.         ELSIF nextCh = TAB THEN Tab(dw, dx); x := 0; y := 0; w := dx; h := 0
  313.         ELSIF R.elem # NIL THEN e := R.elem;
  314.             IF (e IS Parc) & (P.W = 9999 * Unit) THEN (* P gets this value in prepare message *)
  315.                 w := Min(SHORT((P.width + P.left) DIV Unit), F.W - F.right - F.left);
  316.                 e.W := LONG(w) * Unit
  317.             ELSE w := SHORT(e.W DIV Unit)
  318.             END;
  319.             dx := w; x := 0; y := -SHORT(P.dsr DIV Unit); h := SHORT(e.H DIV Unit)
  320.         ELSE Display.GetChar(R.fnt.raster, nextCh, dx, x, y, w, h, pat)
  321.         END
  322.     END GetSpecial;
  323.     PROCEDURE NextLine (T: Texts.Text; VAR org: LONGINT);    (*R, nextCh set; org = Texts.Pos(R)-1*)
  324.         VAR pat: Display.Pattern; pos, bk, d: LONGINT; width, tw, dx, x, y, w, h: INTEGER;
  325.             R1: Texts.Reader; peekCh: CHAR; indent: INTEGER;
  326.     BEGIN
  327.         tw := 0; dx := 0; w := 0; bk := -999;    (* bk = pos of last seperator *)
  328.         pos := org; ParcBefore(T, pos, P, pbeg); width := SHORT(P.width DIV Unit);
  329.         indent := 0;
  330.         IF org > 0 THEN Texts.OpenReader(R1, T, org - 1); Texts.Read(R1, peekCh);
  331.             IF (peekCh = CR) OR (R1.elem # NIL) & (R1.elem IS Parc) THEN indent := SHORT(P.first DIV Unit) END;
  332.         END;
  333.         INC(tw, indent);
  334.         LOOP INC(pos);    (*inv pos = Texts.Pos(R), ~R.eof => nextCh = text[pos-1]*)
  335.             IF R.eot OR (nextCh = CR) THEN EXIT END;
  336.             INC(tw, dx);
  337.             IF nextCh <= " " THEN MeasureSpecial(tw, dx, x, y, w, h)
  338.             ELSE Display.GetChar(R.fnt.raster, nextCh, dx, x, y, w, h, pat)
  339.             END;
  340.             IF tw + x + w > width THEN d := pos - bk;
  341.                 IF (d < AdjustSpan) & (nextCh > " ") THEN pos := bk
  342.                 ELSIF ((nextCh > " ") OR (nextCh = Texts.ElemChar)) & (pos > org + 1) THEN DEC(pos)
  343.                 END;
  344.                 Texts.OpenReader(R, T, pos); Texts.Read(R, nextCh);
  345.                 EXIT
  346.             END;
  347.             IF (nextCh <= " ") & (nextCh # Texts.ElemChar) THEN bk := pos END;
  348.             Texts.Read(R, nextCh)
  349.         END;
  350.         org := pos
  351.     END NextLine;
  352.     PROCEDURE BegOfLine (T: Texts.Text; VAR pos: LONGINT; adjust: BOOLEAN);
  353.         (* returns origin of line containing pos *)
  354.         VAR p, org: LONGINT;
  355.     BEGIN
  356.         IF pos <= 0 THEN pos := 0
  357.         ELSE
  358.             IF pos <= T.len THEN org := pos ELSE org := T.len END;
  359.             LOOP    (*search backwards for CR*)
  360.                 IF org = 0 THEN EXIT END;
  361.                 Texts.OpenReader(R, T, org - 1); Texts.Read(R, nextCh);
  362.                 IF nextCh = CR THEN EXIT END;
  363.                 DEC(org)
  364.             END;
  365.             IF adjust THEN    (*search forward for actual line origin*)
  366.                 Texts.OpenReader(R, T, org); Texts.Read(R, nextCh); p := org;
  367.                 REPEAT org := p; NextLine(T, p) UNTIL (p > pos) OR R.eot
  368.             END;
  369.             pos := org
  370.         END
  371.     END BegOfLine;
  372.     PROCEDURE AdjustMetrics (F: Frame; t: TextLine; VAR pw, tw, ddx, cn: INTEGER);    (*t.org set*)
  373.         (* pw ... x-coord of first char in line (in pixels); tw ... width of text line; ddx, cn ... see GetSpecial *)
  374.     BEGIN
  375.         P := t.P; pbeg := t.pbeg;
  376.         pw := F.left; tw := t.w; ddx := 0; cn := 0;
  377.         IF t.pbeg # t.org THEN
  378.             INC(pw, SHORT((P.left + t.indent) DIV Unit));
  379.             IF leftAdj IN P.opts THEN
  380.                 IF (rightAdj IN P.opts) & (t.nob > 0) THEN
  381.                     tw := SHORT(P.width DIV Unit); ddx := (tw - t.w0) DIV t.nob; cn := (tw - t.w0) MOD t.nob
  382.                 END
  383.             ELSIF rightAdj IN P.opts THEN INC(pw, SHORT(P.width DIV Unit) - t.w0)
  384.             ELSE (*center*) INC(pw, (SHORT(P.width DIV Unit) - t.w0) DIV 2)
  385.             END;
  386.             DEC(tw, SHORT(t.indent DIV Unit));
  387.         END
  388.     END AdjustMetrics;
  389.     (* Screen Placement *)
  390.     PROCEDURE DrawSpecial (F: Frame; px, py, x, y: INTEGER);    (*R, nextCh set*)
  391.         VAR e: Texts.Elem; pat: Display.Pattern; dx, w, h: INTEGER; msg: DisplayMsg;
  392.     BEGIN
  393.         IF (nextCh = TAB) OR (nextCh = CR) THEN (*skip*)
  394.         ELSIF R.elem # NIL THEN e := R.elem;
  395.             IF ~(e IS Parc) OR F.showsParcs THEN
  396.                 msg.prepare := FALSE; msg.fnt := R.fnt; msg.col := R.col; msg.pos := Texts.Pos(R) - 1;
  397.                 msg.frame := F; msg.X0 := px + x; msg.Y0 := py + y; msg.elemFrame := NIL;
  398.                 e.handle(e, msg);
  399.                 IF msg.elemFrame # NIL THEN msg.elemFrame.next := F.dsc; F.dsc := msg.elemFrame END;
  400.             ELSIF pageBreak IN e(Parc).opts THEN (*(e IS Parc) & ~F.showsParcs*)
  401.                 Display.ReplPattern(Display.white, Display.grey1, px + x, py, SHORT(e.W DIV Unit), 1, Display.replace)
  402.             END
  403.         ELSE Display.GetChar(R.fnt.raster, nextCh, dx, x, y, w, h, pat);
  404.             Display.CopyPattern(R.col, pat, px + x, py + y, Display.invert)
  405.         END;
  406.     END DrawSpecial;
  407.     PROCEDURE ShowLine (F: Frame; t: TextLine; left, right, py: INTEGER);
  408.         VAR pat: Display.Pattern; i: LONGINT; n, cn, lm, px, pw, tw, ddx, dx, x, y, w, h: INTEGER;
  409.     BEGIN
  410.         (* lm ... left parc margin in screen coord; pw ...  x of first char in frame coord *)
  411.         Texts.OpenReader(R, F.text, t.org); AdjustMetrics(F, t, pw, tw, ddx, cn);
  412.         lm := F.X + F.left + SHORT(P.left DIV Unit); px := F.X + pw; INC(py, t.dsr); i := 0; n := 0;
  413.         WHILE i < t.len DO Texts.Read(R, nextCh);
  414.             IF nextCh <= " " THEN GetSpecial(F, n, cn, ddx, px - lm, dx, x, y, w, h)
  415.             ELSE Display.GetChar(R.fnt.raster, nextCh, dx, x, y, w, h, pat)
  416.             END;
  417.             INC(y, R.fnt.height * R.voff DIV 64);
  418.             IF px + x + w <= right THEN
  419.                 IF px + x >= left THEN
  420.                     IF nextCh <= " " THEN DrawSpecial(F, px, py, x, y)
  421.                     ELSE 
  422.                         IF (R.col = Display.white) & ((F.col = Display.black) OR (F.col = Display.white)) THEN
  423.                             Display.CopyPattern(R.col, pat, px + x, py + y, Display.invert)
  424.                         ELSE
  425.                             Display.CopyPattern(R.col, pat, px + x, py + y, Display.paint)
  426.                         END
  427.                     END
  428.                 END;
  429.                 INC(px, dx); INC(i)
  430.             ELSE i := t.len
  431.             END
  432.         END
  433.     END ShowLine;
  434.     PROCEDURE ShowLines (F: Frame; botH, topH: INTEGER; erase: BOOLEAN);
  435.         VAR t: TextLine; ph: INTEGER;
  436.     BEGIN
  437.         t := F.trailer.next; ph := F.H - F.top;
  438.         WHILE (t # F.trailer) & (ph - t.h >= topH) DO DEC(ph, t.h); t := t.next END;
  439.         WHILE (t # F.trailer) & (ph - t.h >= botH) DO DEC(ph, t.h);
  440.             IF erase THEN Erase(F, F.X + F.left, F.Y + ph, F.W - F.right - F.left, t.h) END;
  441.             ShowLine(F, t, F.X + F.left, F.X + F.W - F.right, F.Y + ph); t := t.next
  442.         END
  443.     END ShowLines;
  444.     (* Screen Casting *)
  445.     PROCEDURE MeasureLine (F: Frame; maxW: INTEGER; t: TextLine);    (* R, nextCh set *)
  446.         VAR pat: Display.Pattern; len, bklen, d: LONGINT; eol: BOOLEAN;
  447.             nob, bknob, width, minY, bkminY, maxY, bkmaxY, tw, bktw, lsp, dsr, dx, x, y, w, h: INTEGER;
  448.             R1: Texts.Reader; peekCh: CHAR;
  449.             (* bk* ... backup for last blank *)
  450.     BEGIN
  451.         len := 0; nob := 0; bklen := -999; tw := 0; dx := 0; minY := 0; maxY := 0;
  452.         ParcBefore(F.text, t.org, P, pbeg);
  453.         lsp := SHORT(P.lsp DIV Unit); dsr := SHORT(P.dsr DIV Unit); width := SHORT(P.width DIV Unit);
  454.         t.indent := 0;
  455.         IF t.org > 0 THEN Texts.OpenReader(R1, F.text, t.org - 1); Texts.Read(R1, peekCh);
  456.             IF (peekCh = CR) OR (R1.elem # NIL) & (R1.elem IS Parc) THEN t.indent := P.first END;
  457.         END;
  458.         INC(tw, SHORT(t.indent DIV Unit));
  459.         LOOP
  460.             IF R.eot OR (nextCh = CR) THEN nob := 0; eol := ~R.eot; EXIT END;
  461.             IF nextCh <= " " THEN MeasureSpecial(tw, dx, x, y, w, h)
  462.             ELSE Display.GetChar(R.fnt.raster, nextCh, dx, x, y, w, h, pat)
  463.             END;
  464.             IF tw + x + w > width THEN d := len - bklen;
  465.                 IF (d < AdjustSpan) & (nextCh > " ") THEN eol := TRUE;
  466.                     Texts.OpenReader(R, F.text, Texts.Pos(R) - d);
  467.                     nob := bknob; len := bklen; tw := bktw; minY := bkminY; maxY := bkmaxY
  468.                 ELSIF len = 0 THEN    (* force at least one character on each line *)
  469.                     INC(len); INC(y, R.fnt.height * R.voff DIV 64); minY := Min(minY, y); maxY := Max(maxY, y + h);
  470.                     Texts.Read(R, nextCh); eol := FALSE; tw := maxW
  471.                 ELSE eol := (nextCh <= " ") & (nextCh # Texts.ElemChar)
  472.                 END;
  473.                 EXIT
  474.             END;
  475.             IF (nextCh <= " ") & (nextCh # Texts.ElemChar) THEN
  476.                 bknob := nob; bklen := len; bktw := tw; bkminY := minY; bkmaxY := maxY;
  477.                 IF nextCh = " " THEN INC(nob) END
  478.             END;
  479.             INC(len); INC(tw, dx); INC(y, R.fnt.height * R.voff DIV 64);
  480.             IF y < minY THEN minY := y END;
  481.             IF y + h > maxY THEN maxY := y + h END;
  482.             Texts.Read(R, nextCh)
  483.         END;
  484.         IF ~F.showsParcs & (pbeg = t.org) THEN dsr := 0; t.h := SHORT(P.lead DIV Unit) + 1
  485.         ELSIF gridAdj IN P.opts THEN
  486.             WHILE dsr < -minY DO INC(dsr, lsp) END;
  487.             t.h := Max(lsp, dsr + maxY); INC(t.h, (-t.h) MOD lsp)
  488.         ELSE dsr := Max(dsr, -minY); t.h := Max(lsp, dsr + maxY)
  489.         END;
  490.         t.len := len; t.w0 := tw; t.w := Min(tw, maxW); t.dsr := dsr; t.nob := nob; t.eot := R.eot; t.P := P; t.pbeg := pbeg;
  491.         IF eol THEN Texts.Read(R, nextCh); t.span := len + 1 ELSE t.span := len END
  492.     END MeasureLine;
  493.     PROCEDURE MeasureLines (F: Frame; org: LONGINT; VAR trailer: TextLine);
  494.         VAR s, t: TextLine; ph: INTEGER;
  495.     BEGIN
  496.         NEW(trailer); s := trailer;
  497.         Texts.OpenReader(R, F.text, org); Texts.Read(R, nextCh); ph := F.H - F.top;
  498.         LOOP NEW(t); t.org := org; MeasureLine(F, F.W - F.left - F.right, t);
  499.             IF ph - t.h < F.bot THEN EXIT END;
  500.             s.next := t; s := t; INC(org, s.span); DEC(ph, s.h);
  501.             IF R.eot THEN EXIT END
  502.         END;
  503.         s.next := trailer; trailer.eot := TRUE; trailer.org := org; (* start of first invisible line *) trailer.len := 0; trailer.w := 0;
  504.         trailer.h := SHORT(defParc.lsp DIV Unit); trailer.P := P (* P set by MeasureLine *) ; trailer.pbeg := pbeg
  505.     END MeasureLines;
  506.     (** Locators **)
  507.     PROCEDURE LocateLineTop (F: Frame; trailer: TextLine; org: LONGINT; VAR loc: Location);
  508.         VAR t: TextLine; ph: INTEGER;
  509.     BEGIN
  510.         ph := F.H - F.top; t := trailer.next;
  511.         WHILE (t # trailer) & (t.org # org) DO DEC(ph, t.h); t := t.next END;
  512.         loc.org := org; loc.line := t; loc.y := F.Y + ph
  513.     END LocateLineTop;
  514.     PROCEDURE Width (F: Frame; t: TextLine; pos: LONGINT; VAR pw, dx, dy: INTEGER);
  515.         VAR pat: Display.Pattern; i: LONGINT; n, mw, lm, tw, ddx, cn, x, y, w, h: INTEGER;
  516.     BEGIN
  517.         AdjustMetrics(F, t, pw, tw, ddx, cn); dy := 0; lm := F.left + SHORT(P.left DIV Unit);
  518.         IF t # F.trailer THEN Texts.OpenReader(R, F.text, t.org); Texts.Read(R, nextCh);
  519.             i := 0; n := 0; DEC(pos, t.org); dx := 0; mw := F.W - F.right;
  520.             WHILE ~R.eot & (i < t.len) & (i <= pos) & (pw + dx <= mw) DO
  521.                 (* i ... pos of nextCh; dx ... width of char before nextCh; pw ... line width up to pos (or up to right margin) *)
  522.                 INC(i); INC(pw, dx);
  523.                 IF nextCh <= " " THEN GetSpecial(F, n, cn, ddx, pw - lm, dx, x, y, w, h)
  524.                 ELSE Display.GetChar(R.fnt.raster, nextCh, dx, x, y, w, h, pat)
  525.                 END;
  526.                 dy := R.fnt.height * R.voff DIV 64;
  527.                 Texts.Read(R, nextCh)
  528.             END;
  529.             IF (i <= pos) & (pw + dx <= mw) THEN INC(i); INC(pw, dx) END
  530.         ELSE dx := 4
  531.         END
  532.     END Width;
  533.     PROCEDURE LocatePos* (F: Frame; pos: LONGINT; VAR loc: Location);    (* loc.dx = dx of char at pos *)
  534.         VAR t: TextLine; pw, dx, dy: INTEGER;
  535.     BEGIN
  536.         IF pos < F.org THEN pos := F.org; t := F.trailer.next
  537.         ELSIF pos < F.trailer.org THEN t := F.trailer;
  538.             WHILE (t.next # F.trailer) & (t.next.org <= pos) DO t := t.next END
  539.         ELSE pos := F.trailer.org; t := F.trailer.next;
  540.             WHILE ~t.eot DO t := t.next END
  541.         END;
  542.         Width(F, t, pos, pw, dx, dy); LocateLineTop(F, F.trailer, t.org, loc); DEC(loc.y, loc.line.h);
  543.         loc.org := t.org; loc.pos := pos; loc.x := F.X + pw; loc.dx := dx; loc.dy := dy; loc.line := t
  544.     END LocatePos;
  545.     PROCEDURE LocateLine* (F: Frame; y: INTEGER; VAR loc: Location);
  546.         (* loc.x = line start; loc.y = line bottom; loc.dx = line width *)
  547.         VAR t: TextLine; h, ph, pw, tw, ddx, cn: INTEGER;
  548.     BEGIN
  549.         t := F.trailer.next; h := y - F.Y; ph := F.H - F.top - t.h;
  550.         WHILE ~t.eot & (ph - t.next.h >= F.bot) & (ph > h) DO t := t.next; DEC(ph, t.h) END;
  551.         AdjustMetrics(F, t, pw, tw, ddx, cn);
  552.         IF pw >= F.W - F.right THEN pw := F.W - F.right - 4 END;
  553.         loc.org := t.org; loc.pos := loc.org; loc.x := F.X + pw; loc.y := F.Y + ph; loc.dx := tw; loc.dy := 0; loc.line := t
  554.     END LocateLine;
  555.     PROCEDURE LocateChar* (F: Frame; x, y: INTEGER; VAR loc: Location);
  556.         VAR t: TextLine; pat: Display.Pattern; i: LONGINT; n, w, lm, pw, tw, ddx, cn, dx, xc, yc, wc, hc: INTEGER;
  557.     BEGIN
  558.         LocateLine(F, y, loc); t := loc.line; w := x - F.X; AdjustMetrics(F, t, pw, tw, ddx, cn);
  559.         lm := F.left + SHORT(P.left DIV Unit);
  560.         IF (t # F.trailer) & (w > pw) THEN Texts.OpenReader(R, F.text, t.org);
  561.             i := 0; n := 0; dx := 0; nextCh := 0X;
  562.             WHILE (i < t.len) & (pw + dx < w) DO
  563.                 (* i = pos after nextCh; dx = width of nextCh; pw = line width without nextCh *)
  564.                 Texts.Read(R, nextCh); INC(i); INC(pw, dx);
  565.                 IF nextCh <= " " THEN GetSpecial(F, n, cn, ddx, pw - lm, dx, xc, yc, wc, hc)
  566.                 ELSE Display.GetChar(R.fnt.raster, nextCh, dx, xc, yc, wc, hc, pat)
  567.                 END
  568.             END;
  569.             IF pw + dx < w THEN INC(i); INC(pw, dx); R.elem := NIL END;
  570.             INC(loc.pos, i - 1); loc.x := F.X + pw;
  571.             IF i < t.len THEN loc.dx := dx; loc.dy := R.fnt.height * R.voff DIV 64 ELSE loc.dx := 4 END
  572.         ELSE loc.dx := 4; R.elem := NIL
  573.         END
  574.     END LocateChar;
  575.     PROCEDURE LocateWord* (F: Frame; x, y: INTEGER; VAR loc: Location);
  576.         VAR t: TextLine; pos, i: LONGINT; px, rx: INTEGER; pat: Display.Pattern; dx, xc, yc, wc, hc: INTEGER;
  577.     BEGIN
  578.         LocateChar(F, x, y, loc); pos := loc.pos + 1;
  579.         REPEAT DEC(pos); Texts.OpenReader(R, F.text, pos); Texts.Read(R, nextCh)
  580.         UNTIL (pos < loc.org) OR (nextCh > " ");
  581.         INC(pos);
  582.         REPEAT DEC(pos); Texts.OpenReader(R, F.text, pos); Texts.Read(R, nextCh)
  583.         UNTIL (pos < loc.org) OR (nextCh <= " ");
  584.         LocatePos(F, pos + 1, loc); t := loc.line; i := loc.pos - loc.org;
  585.         IF i < t.len THEN px := loc.x; rx := F.X + F.W - F.right;
  586.             Texts.OpenReader(R, F.text, loc.pos); dx := 0; wc := 0; nextCh := "x";
  587.             WHILE (i < t.len) & (nextCh > " ") & (px + dx < rx) DO
  588.                 Texts.Read(R, nextCh); INC(i); INC(px, dx);
  589.                 Display.GetChar(R.fnt.raster, nextCh, dx, xc, yc, wc, hc, pat)
  590.             END;
  591.             IF (nextCh > " ") & (px + dx < rx) THEN INC(i); INC(px, dx) END;
  592.             loc.dx := px - loc.x
  593.         ELSE loc.dx := 0
  594.         END
  595.     END LocateWord;
  596.     PROCEDURE Pos* (F: Frame; x, y: INTEGER): LONGINT;
  597.         VAR loc: Location;
  598.     BEGIN LocateChar(F, x, y, loc); RETURN loc.pos
  599.     END Pos;
  600.     PROCEDURE ThisSubFrame (F: Frame; x, y: INTEGER): Display.Frame;
  601.         VAR f: Display.Frame;
  602.     BEGIN f := F.dsc;
  603.         WHILE (f # NIL) & ((x < f.X) OR (x >= f.X + f.W) OR (y < f.Y) OR (y >= f.Y + f.H)) DO f := f.next END;
  604.         RETURN f
  605.     END ThisSubFrame;
  606.     (** Caret & Selection **)
  607.     PROCEDURE PassSubFocus (F: Frame; f: Display.Frame);
  608.         (* pass focus from F.focus to f (f is also an element frame in F) *)
  609.         VAR loc: Location; f1: Display.Frame; ctrl: Oberon.ControlMsg; focus: FocusMsg;
  610.     BEGIN
  611.         IF F.focus # NIL THEN f1 := F.focus;
  612.             ctrl.id := Oberon.defocus; f1.handle(f1, ctrl);
  613.             LocateChar(F, f1.X + 1, f1.Y + 1, loc);
  614.             InvertBorder(f1); F.focus := NIL;
  615.             IF R.elem # NIL THEN
  616.                 focus.focus := FALSE; focus.elemFrame := f1; focus.frame := F; R.elem.handle(R.elem, focus)
  617.             END
  618.         END;
  619.         IF f # NIL THEN
  620.             LocateChar(F, f.X + 1, f.Y + 1, loc);    (* side effect: set R to element *)
  621.             focus.focus := TRUE; focus.elemFrame := f; focus.frame := F; R.elem.handle(R.elem, focus);
  622.             InvertBorder(f)
  623.         END;
  624.         F.focus := f
  625.     END PassSubFocus;
  626.     PROCEDURE RemoveSelection* (F: Frame);
  627.     BEGIN
  628.         IF F.hasSel THEN InvertSelection(F, F.selbeg, F.selend); F.hasSel := FALSE END
  629.     END RemoveSelection;
  630.     PROCEDURE SetSelection* (F: Frame; beg, end: LONGINT);    (** forces range to visible bounds **)
  631.         VAR loc: Location;
  632.     BEGIN
  633.         IF end > F.text.len THEN end := F.text.len END;
  634.         IF end > beg THEN
  635.             IF F.hasSel & (F.selbeg.pos = beg) THEN
  636.                 IF (F.selend.pos < end) & (F.selend.pos < F.trailer.org) THEN
  637.                     LocatePos(F, F.selend.pos, loc); LocatePos(F, end, F.selend); InvertSelection(F, loc, F.selend)
  638.                 ELSIF end < F.selend.pos THEN
  639.                     LocatePos(F, end, loc); InvertSelection(F, loc, F.selend); LocatePos(F, end, F.selend)
  640.                 END
  641.             ELSE RemoveSelection(F); PassSubFocus(F, NIL);
  642.                 LocatePos(F, beg, F.selbeg); LocatePos(F, end, F.selend); InvertSelection(F, F.selbeg, F.selend)
  643.             END;
  644.             F.hasSel := TRUE; F.time := Oberon.Time()
  645.         END
  646.     END SetSelection;
  647.     PROCEDURE RemoveCaret* (F: Frame);
  648.         VAR msg: Oberon.ControlMsg;
  649.     BEGIN
  650.         IF F.focus # NIL THEN msg.id := Oberon.defocus; F.focus.handle(F.focus, msg) END;
  651.         IF F.hasCar THEN InvertCaret(F); F.hasCar := FALSE END
  652.     END RemoveCaret;
  653.     PROCEDURE SetCaret* (F: Frame; pos: LONGINT);    (** only done if within visible bounds **)
  654.     BEGIN
  655.         IF ~F.hasCar OR (F.carloc.pos # pos) THEN RemoveCaret(F); PassSubFocus(F, NIL);
  656.             LocatePos(F, pos, F.carloc);
  657.             IF F.carloc.x <= F.X + F.W - F.right THEN InvertCaret(F); F.hasCar := TRUE END
  658.         END
  659.     END SetCaret;
  660.     (** Display Range **)
  661.     PROCEDURE Complete (F: Frame; trailer: TextLine; s: TextLine; org: LONGINT; ph: INTEGER);
  662.         VAR u: TextLine;
  663.     BEGIN
  664.         IF ph > F.bot THEN    (*try to add new lines to the bottom*)
  665.             Texts.OpenReader(R, F.text, org); Texts.Read(R, nextCh);
  666.             LOOP
  667.                 IF R.eot THEN EXIT END;
  668.                 NEW(u); u.org := org; MeasureLine(F, F.W - F.left - F.right, u);
  669.                 IF ph - u.h < F.bot THEN EXIT END;
  670.                 s.next := u; s := s.next; DEC(ph, s.h); INC(org, s.span)
  671.             END
  672.         END;
  673.         s.next := trailer; trailer.eot := TRUE; trailer.org := org; trailer.len := 0; trailer.w := 0;
  674.         trailer.h := SHORT(defParc.lsp DIV Unit); trailer.P := P; trailer.pbeg := pbeg
  675.     END Complete;
  676.     PROCEDURE ShowFrom (F: Frame; pos: LONGINT);    (* removes global marks as needed and neutralizes F *)
  677.         VAR new, s: TextLine; beg, end: Location; org: LONGINT; ph, y0, dy: INTEGER;
  678.     BEGIN
  679.         F.handle(F, neutralize);
  680.         IF (F.trailer # NIL) & (F.org < pos) & (pos < F.trailer.org) THEN    (* shift up and extend to the bottom *)
  681.             LocateLineTop(F, F.trailer, pos, beg); LocateLineTop(F, F.trailer, F.trailer.org, end);
  682.             dy := (F.Y + F.H - F.top) - beg.y; Shift(F, end.y, end.y + dy, beg.y - end.y);
  683.             Erase(F, F.X + F.left, end.y, F.W - F.left, dy);
  684.             s := F.trailer.next; WHILE s.org # pos DO s := s.next END;
  685.             F.trailer.next := s; org := s.org + s.span; ph := F.H - F.top - s.h;
  686.             WHILE s.next # F.trailer DO s := s.next; org := org + s.span; ph := ph - s.h END;
  687.             Complete(F, F.trailer, s, org, ph); F.org := pos; ShowLines(F, F.bot, end.y + dy - F.Y, FALSE)
  688.         ELSIF (F.trailer = NIL) OR (pos # F.org) THEN
  689.             MeasureLines(F, pos, new);
  690.             IF (F.trailer # NIL) & (pos < F.org) & (F.org <= new.org) THEN    (* shift down and extend to the top *)
  691.                 LocateLineTop(F, new, F.org, beg); LocateLineTop(F, new, new.org, end);
  692.                 y0 := F.Y + F.H - F.top; Shift(F, y0 - (beg.y - end.y), end.y, beg.y - end.y);
  693.                 Erase(F, F.X + F.left, beg.y, F.W - F.left, y0 - beg.y);
  694.                 Erase(F, F.X + F.left, F.Y + F.bot, F.W - F.left, end.y - (F.Y + F.bot));
  695.                 F.org := pos; F.trailer := new; ShowLines(F, beg.y - F.Y, F.H - F.top, FALSE)
  696.             ELSE    (* full redisplay *)
  697.                 IF F.trailer = NIL THEN Erase(F, F.X, F.Y, F.W, F.H); ShowBar(F, 0, F.H); F.markH := -1
  698.                 ELSE Erase(F, F.X + F.left, F.Y + F.bot, F.W - F.left, F.H - F.bot - F.top)
  699.                 END;
  700.                 F.org := pos; F.trailer := new; ShowLines(F, F.bot, F.H - F.top, FALSE)
  701.             END
  702.         END;
  703.         ShowTick(F)
  704.     END ShowFrom;
  705.     PROCEDURE Show* (F: Frame; pos: LONGINT);    (** removes global marks as needed and neutralizes F **)
  706.     BEGIN BegOfLine(F.text, pos, TRUE); ShowFrom(F, pos)
  707.     END Show;
  708.     PROCEDURE Resize (F: Frame; x, y, w, h: INTEGER);
  709.         VAR oldY, oldH, dh, ph: INTEGER; t: TextLine;
  710.     BEGIN
  711.         IF (w = 0) OR (h = 0) THEN InvalSubFrames(F, F.X, F.Y, F.W, F.H);
  712.             F.X := x; F.Y := y; F.W := w; F.H := h; F.trailer := NIL
  713.         ELSIF (F.trailer # NIL) & (x = F.X) & (w = F.W) THEN
  714.             oldY := F.Y; oldH := F.H; Tick(F); F.markH := -1; F.Y := y; F.H := h;
  715.             IF h > oldH THEN dh := h - oldH;    (* extend *)
  716.                 IF y + h # oldY + oldH THEN
  717.                     Display.CopyBlock(x, oldY, w, oldH, x, y + dh, Display.replace);
  718.                     ShiftSubFrames(F, oldY, y + dh, oldH)
  719.                 END;
  720.                 EraseRect(F, x, y, w, dh); ShowBar(F, 0, dh);
  721.                 t := F.trailer; ph := F.H - F.top;
  722.                 WHILE t.next # F.trailer DO t := t.next; ph := ph - t.h END;
  723.                 Complete(F, F.trailer, t, F.trailer.org, ph); ShowLines(F, F.bot, ph, FALSE)
  724.             ELSE dh := oldH - h;    (* reduce *)
  725.                 IF y + h # oldY + oldH THEN
  726.                     Display.CopyBlock(x, oldY + dh, w, h, x, y, Display.replace);
  727.                     ShiftSubFrames(F, oldY + dh, y, h)
  728.                 END;
  729.                 t := F.trailer; ph := F.H - F.top;
  730.                 WHILE (t.next # F.trailer) & (ph - t.next.h >= F.bot) DO t := t.next; DEC(ph, t.h) END;
  731.                 IF t = F.trailer THEN t.org := F.org; t.span := 0 END;
  732.                 Complete(F, F.trailer, t, t.org + t.span, ph);
  733.                 EraseRect(F, x + F.left, y, w - F.left, ph);
  734.                 InvalSubFrames(F, x, oldY, w, y - oldY); InvalSubFrames(F, x, y + h, w, dh - (y - oldY))
  735.             END;
  736.             ShowTick(F)
  737.         ELSE F.X := x; F.Y := y; F.W := w; F.H := h; F.trailer := NIL; Show(F, F.org)
  738.         END
  739.     END Resize;
  740.     (** Contents Update **)
  741.     PROCEDURE Update (F: Frame; VAR msg: UpdateMsg);    (** removes global marks as needed **)
  742.         VAR t: TextLine; org, d: LONGINT;
  743.             foc: Display.Frame; beg, end: LONGINT; ch: CHAR; r: Texts.Reader; loc: Location;
  744.         PROCEDURE Begin (VAR beg: LONGINT; VAR org0: LONGINT; VAR q: TextLine);
  745.             (* org0 = origin of first affected line; beg = pos of first modified character; q = first affected line (if line origin has not moved).*)
  746.             (* q = NIL => beg = org0; q # NIL => first (beg-org0) characters of q need not be redrawn *)
  747.             VAR trailer, t: TextLine;
  748.         BEGIN
  749.             trailer := F.trailer; t := trailer;
  750.             WHILE (t.next # trailer) & (beg >= t.next.org + t.next.span) & ~t.next.eot DO t := t.next END;
  751.             q := t.next; org0 := beg; BegOfLine(F.text, org0, TRUE);
  752.             IF (org0 # q.org) OR (q = trailer) THEN
  753.                 IF org0 > q.org THEN org0 := q.org END;
  754.                 beg := org0; q := NIL
  755.             END
  756.         END Begin;
  757.         PROCEDURE Adjust (end, delta: LONGINT);
  758.             (* H1 = top of synchronization line in old frame *)
  759.             (* h0 = top of line that was modified *)
  760.             (* h1 = top of block in new frame that could be reused *)
  761.             (* h2 = bottom of last line in new frame *)
  762.             (* h1 - h2 = height of block that could be reused *)
  763.             VAR new, old, s, t, u, p, q: TextLine; bot: Location;
  764.                 org, org0, beg: LONGINT; ph, h0, h1, H1, h2, lm, dx, dy: INTEGER;
  765.         BEGIN
  766.             q := NIL; LocateLineTop(F, F.trailer, F.trailer.org, bot);
  767.             IF msg.beg < F.org THEN org0 := F.org; beg := org0 ELSE beg := msg.beg; Begin(beg, org0, q) END;
  768.             NEW(new); s := new; old := F.trailer; t := old; org := F.org; ph := F.H - F.top;
  769.             WHILE (t.next # old) & (t.next.org # org0) DO t := t.next;    (*transfer unchanged prefix*)
  770.                 s.next := t; s := t; DEC(ph, s.h); INC(org, s.span)
  771.             END;
  772.             h0 := ph; H1 := h0; t := t.next; p := s;
  773.             Texts.OpenReader(R, F.text, org); Texts.Read(R, nextCh);    (*rebuild at least one line descriptor*)
  774.             LOOP NEW(u); u.org := org; MeasureLine(F, F.W - F.left - F.right, u);
  775.                 IF ph - u.h < F.bot THEN h1 := ph; h2 := h1; EXIT END;
  776.                 s.next := u; s := s.next; DEC(ph, s.h); INC(org, s.span);
  777.                 IF R.eot THEN h1 := ph; h2 := h1; EXIT END;
  778.                 IF org > end THEN
  779.                     WHILE (t # old) & (org > t.org + delta) DO DEC(H1, t.h); t := t.next END;
  780.                     IF (org = t.org + delta) & (P = t.P) THEN h1 := ph;    (*resynchronized*)
  781.                         WHILE (t # old) & (ph - t.h >= F.bot) DO    (*transfer unchanged suffix*)
  782.                             s.next := t; s := t; s.org := org; ParcBefore(F.text, s.org, s.P, s.pbeg);
  783.                             DEC(ph, s.h); INC(org, s.span); t := t.next
  784.                         END;
  785.                         h2 := ph; EXIT
  786.                     END
  787.                 END
  788.             END;
  789.             Shift(F, F.Y + H1 - (h1 - h2), F.Y + h2, h1 - h2);
  790.             Complete(F, new, s, org, ph); F.trailer := new; t := p.next;
  791.             IF (q # NIL) & (q.h = t.h) & (q.dsr = t.dsr) & (q.org = t.org) & (q.P = t.P) & (end <= t.org + t.span) THEN
  792.                 P := t.P; pbeg := t.pbeg;
  793.                 IF (P.opts * AdjMask = {leftAdj}) OR (P.opts * AdjMask = AdjMask) & (q.nob = 0) & (t.nob = 0) THEN
  794.                     Width(F, t, beg-1, lm, dx, dy);    (*preserve prefix of first affected line*) (* -1 to get chars with leading pixels right *)
  795.                     DEC(h0, t.h); Erase(F, F.X + lm, F.Y + h0, F.W - lm, t.h);
  796.                     ShowLine(F, t, F.X + lm, F.X + F.W - F.right, F.Y + h0)
  797.                 END
  798.             END;
  799.             ShowLines(F, h1, h0, TRUE);
  800.             Erase(F, F.X + F.left, bot.y, F.W - F.left, h2 - (bot.y - F.Y)); ShowLines(F, F.bot, h2, FALSE)
  801.         END Adjust;
  802.     BEGIN 
  803.         foc := F.focus; beg := msg.beg; end := msg.end; 
  804.         F.handle(F, neutralize); MarkMenu(F);
  805.         IF (msg.id = Texts.insert) & (msg.beg < F.org) THEN t := F.trailer; d := msg.end - msg.beg; INC(F.org, d);
  806.             REPEAT INC(t.org, d); t := t.next UNTIL t = F.trailer
  807.         ELSIF (msg.id = Texts.delete) & (msg.end <= F.org) THEN t := F.trailer; d := msg.end - msg.beg; DEC(F.org, d);
  808.             REPEAT DEC(t.org, d); t := t.next UNTIL t = F.trailer
  809.         END;
  810.         org := F.org;
  811.         IF msg.beg <= F.org + AdjustSpan THEN BegOfLine(F.text, org, TRUE) END;
  812.         ParcBefore(F.text, org, P, d);
  813.         IF (org # F.org) OR (P # F.trailer.next.P) THEN
  814.             F.trailer := NIL; Show(F, F.org)
  815.         ELSIF (msg.end > org) & (msg.beg < F.trailer.org + AdjustSpan) THEN
  816.             IF msg.id = Texts.replace THEN Adjust(msg.end, 0);
  817.                 (* refocus element if necessary *)
  818.                 IF (foc # NIL) & (end-beg = 1) THEN
  819.                     Texts.OpenReader(r, F.text, beg); Texts.Read(r, ch);
  820.                     IF r.elem # NIL THEN
  821.                         LocatePos(F, beg, loc); foc := ThisSubFrame(F, loc.x, loc.y); PassSubFocus(F, foc);
  822.                     END
  823.                 END
  824.             ELSIF msg.id = Texts.insert THEN Adjust(msg.end, msg.end - msg.beg)
  825.             ELSIF msg.id = Texts.delete THEN Adjust(msg.beg, msg.beg - msg.end)
  826.             END
  827.         END;
  828.         ShowTick(F)
  829.     END Update;
  830.     (** User Interface **)
  831.     PROCEDURE Back (F: Frame; dY: INTEGER; (*inout*) VAR org: LONGINT);    (* mh 10.10.92 *)
  832.         (* computes new org such that old org is (at most) dY pixels below new org *)
  833.         VAR H: INTEGER; oldOrg: LONGINT;
  834.         PROCEDURE TotalHeight (org1, org2: LONGINT): INTEGER;
  835.             (* measures total height of text-lines starting at org1 and ending at the line before the line containing org2 *) 
  836.             VAR h: INTEGER; line: TextLine;
  837.         BEGIN
  838.             Texts.OpenReader(R, F.text, org1); Texts.Read(R, nextCh); NEW(line); h := 0;
  839.             LOOP line.org := org1;
  840.                 MeasureLine(F, F.W - F.left - F.right, line); INC(org1, line.span);
  841.                 IF Texts.Pos(R)-1 > org2 THEN EXIT END;
  842.                 INC(h, line.h);
  843.                 IF R.eot THEN EXIT END;
  844.             END;
  845.             RETURN h
  846.         END TotalHeight;
  847.         PROCEDURE Forward (h: INTEGER);
  848.             (* increase org by n text-lines such that the sum of the n line-heights > h *)
  849.             VAR line: TextLine;
  850.         BEGIN
  851.             Texts.OpenReader(R, F.text, org); Texts.Read(R, nextCh); NEW(line);
  852.             WHILE h > 0 DO line.org := org;
  853.                 MeasureLine(F, F.W - F.left - F.right, line); INC(org, line.span); DEC(h, line.h);
  854.             END;
  855.             org := Texts.Pos(R)-1;
  856.         END Forward;
  857.     BEGIN H := 0;
  858.         LOOP oldOrg := org;
  859.             IF org = 0 THEN EXIT END;
  860.             DEC(org, 800); BegOfLine(F.text, org, FALSE);
  861.             INC(H, TotalHeight(org, oldOrg));
  862.             IF H > dY THEN EXIT END;
  863.         END;
  864.         Forward(H - dY);
  865.     END Back;
  866.     PROCEDURE TrackLine* (F: Frame; VAR x, y: INTEGER; VAR org: LONGINT; VAR keysum: SET);
  867.         VAR keys: SET; new, old: Location;
  868.     BEGIN
  869.         LocateLine(F, y, old); InvertRect(F, old.x, old.y, old.dx + 4, 2); keysum := {};
  870.         REPEAT TrackMouse(x, y, keys, keysum); LocateLine(F, y, new);
  871.             IF new.org # old.org THEN
  872.                 InvertRect(F, new.x, new.y, new.dx + 4, 2); InvertRect(F, old.x, old.y, old.dx + 4, 2); old := new
  873.             END
  874.         UNTIL keys = {};
  875.         InvertRect(F, new.x, new.y, new.dx + 4, 2); org := new.org
  876.     END TrackLine;
  877.     PROCEDURE TrackWord* (F: Frame; VAR x, y: INTEGER; VAR pos: LONGINT; VAR keysum: SET);
  878.         VAR keys: SET; new, old: Location;
  879.     BEGIN 
  880.         LocateWord(F, x, y, old); InvertRect(F, old.x, old.y, old.dx, 2); keysum := {};
  881.         REPEAT TrackMouse(x, y, keys, keysum); LocateWord(F, x, y, new);
  882.             IF new.pos # old.pos THEN
  883.                 InvertRect(F, new.x, new.y, new.dx, 2); InvertRect(F, old.x, old.y, old.dx, 2); old := new
  884.             END
  885.         UNTIL keys = {};
  886.         InvertRect(F, new.x, new.y, new.dx, 2); pos := new.pos
  887.     END TrackWord;
  888.     PROCEDURE TrackCaret* (F: Frame; VAR x, y: INTEGER; VAR keysum: SET);
  889.         VAR keys: SET;
  890.     BEGIN keysum := {};
  891.         REPEAT TrackMouse(x, y, keys, keysum); SetCaret(F, Pos(F, x, y)) UNTIL keys = {}
  892.     END TrackCaret;
  893.     PROCEDURE TrackSelection* (F: Frame; VAR x, y: INTEGER; VAR keysum: SET);
  894.         VAR keys: SET; pos: LONGINT; V: Viewers.Viewer; f: Frame;
  895.     BEGIN
  896.         V := Viewers.This(F.X, F.Y); V := V.next(Viewers.Viewer);
  897.         IF (V.dsc # NIL) & (V.dsc.next # NIL) & (V.dsc.next IS Frame) THEN f := V.dsc.next(Frame);
  898.             IF f.hasSel & (f.text = F.text) THEN
  899.                 IF (f.selbeg.pos < f.trailer.org) & (f.org < f.selend.pos) & (f.selbeg.pos <= Pos(F, x, y)) THEN
  900.                     SetSelection(F, f.selbeg.pos, Pos(F, x, y) + 1)
  901.                 ELSE RemoveSelection(f); f := NIL
  902.                 END
  903.             ELSE f := NIL
  904.             END
  905.         ELSE f := NIL
  906.         END;
  907.         IF f = NIL THEN
  908.             IF F.hasSel & (F.selbeg.pos + 1 = F.selend.pos) & (Pos(F, x, y) = F.selbeg.pos) THEN
  909.                 SetSelection(F, F.selbeg.org, Pos(F, x, y) + 1)
  910.             ELSE SetSelection(F, Pos(F, x, y), Pos(F, x, y) + 1)
  911.             END
  912.         END;
  913.         keysum := {};
  914.         REPEAT TrackMouse(x, y, keys, keysum);
  915.             IF F.hasSel THEN
  916.                 pos := Pos(F, x, Min(y, F.selbeg.y)) + 1;
  917.                 IF pos <= F.selbeg.pos THEN pos := F.selbeg.pos + 1 END;
  918.                 SetSelection(F, F.selbeg.pos, pos);
  919.                 IF f # NIL THEN SetSelection(f, f.selbeg.pos, pos); f.selend.pos := F.selend.pos END
  920.             ELSE SetSelection(F, Pos(F, x, y), Pos(F, x, y) + 1)
  921.             END
  922.         UNTIL keys = {};
  923.         IF f # NIL THEN F.selbeg.pos := f.selbeg.pos END
  924.     END TrackSelection;
  925.     PROCEDURE Call (F: Frame; pos: LONGINT; new: BOOLEAN);
  926.         VAR S: Texts.Scanner; res, i, j: INTEGER;
  927.     BEGIN
  928.         Texts.OpenScanner(S, F.text, pos); Texts.Scan(S);
  929.         IF (S.class = Texts.Name) & (S.line = 0) THEN i := 0;
  930.             WHILE (i < S.len) & (S.s[i] # ".") DO INC(i) END;
  931.             j := i + 1;
  932.             WHILE (j < S.len) & (S.s[j] # ".") DO INC(j) END;
  933.             IF (j >= S.len) & (S.s[i] = ".") THEN
  934.                 par.vwr := Viewers.This(F.X, F.Y);
  935.                 par.frame := F; par.text := F.text; par.pos := pos + S.len;
  936.                 Oberon.Call(S.s, par, new, res);
  937.                 IF res > 0 THEN
  938.                     Texts.WriteString(W0, "Call error: "); Texts.WriteString(W0, Modules.importing);    (* mf *)
  939.                     IF res=1 THEN Texts.WriteString(W0, " not found")
  940.                     ELSIF res=2 THEN Texts.WriteString(W0, " not a valid object file")
  941.                     ELSIF res=3 THEN Texts.WriteString(W0, " imports "); Texts.WriteString(W0, Modules.imported);
  942.                         Texts.WriteString(W0, " with bad key")
  943.                     ELSIF res=4 THEN Texts.WriteString(W0, " not enough memory")
  944.                     ELSIF res=5 THEN Texts.WriteString(W0, " module not found")
  945.                     ELSIF res=6 THEN
  946.                         IF Modules.importing[0]#CHR(0) THEN Texts.WriteString(W0, " command not found")
  947.                         ELSE Texts.OpenWriter (W0);
  948.                         END
  949.                     ELSE Texts.WriteString(W0, " res = "); Texts.WriteInt(W0, res, 0)
  950.                     END
  951.                 ELSIF res < 0 THEN
  952.                     IF i + 1 = S.len THEN Texts.OpenWriter(W0); res := 0  (*execution of module body*)
  953.                     ELSE
  954.                         INC(i); WHILE i < S.len DO Texts.Write(W0, S.s[i]); INC(i) END;
  955.                         Texts.WriteString(W0, " not found")
  956.                     END
  957.                 END;
  958.                 IF res # 0 THEN Texts.WriteLn(W0); Texts.Append(Oberon.Log, W0.buf) END
  959.             END
  960.         END
  961.     END Call;
  962.     PROCEDURE PickAttributes (VAR W: Texts.Writer; T: Texts.Text; pos: LONGINT);
  963.         VAR R: Texts.Reader; ch: CHAR;
  964.     BEGIN
  965.         IF T.len > 0 THEN
  966.             IF pos > 0 THEN Texts.OpenReader(R, T, pos-1); Texts.Read(R, ch)
  967.             ELSE Texts.OpenReader(R, T, 0); Texts.Read(R, ch)
  968.             END;
  969.             Texts.SetFont(W, R.fnt); Texts.SetColor(W, R.col); Texts.SetOffset(W, R.voff);
  970.         ELSE Texts.SetFont(W, Oberon.CurFnt); Texts.SetColor(W, Oberon.CurCol); Texts.SetOffset(W, Oberon.CurOff)
  971.         END
  972.     END PickAttributes;
  973.     PROCEDURE ShiftBlock (F: Frame; delta: INTEGER);    (* shift selected lines to left or right *)
  974.         VAR text: Texts.Text; pos, beg, end, time: LONGINT; select: SelectMsg; ch: CHAR;
  975.     BEGIN
  976.         Oberon.GetSelection(text, beg, end, time);
  977.         IF (time >= 0) & (text = F.text) THEN BegOfLine(F.text, beg, FALSE); pos := beg;
  978.             WHILE pos < end DO Texts.OpenReader(R, F.text, pos); Texts.Read(R, ch);
  979.                 WHILE (R.elem # NIL) & (R.elem IS Parc) & (pos < end) DO Texts.Read(R, ch); INC(pos) END;
  980.                 IF pos < end THEN
  981.                     IF delta < 0 THEN
  982.                         IF (ch <= " ") & (ch # CR) & (ch # Texts.ElemChar) THEN
  983.                             Texts.Delete(F.text, pos, pos + 1); DEC(end)
  984.                         END
  985.                     ELSE
  986.                         PickAttributes(W, text, pos);
  987.                         IF (ch <= " ") & (ch # CR) & (ch # Texts.ElemChar) THEN Texts.Write(W, ch)    (* first char extension *)
  988.                         ELSE Texts.Write(W, TAB)
  989.                         END;
  990.                         Texts.Insert(F.text, pos, W.buf); INC(end); INC(pos)
  991.                     END;
  992.                     Texts.OpenReader(R, F.text, pos);
  993.                     REPEAT Texts.Read(R, ch) UNTIL R.eot OR (ch = CR);
  994.                     pos := Texts.Pos(R)
  995.                 END
  996.             END;
  997.             select.text := F.text; select.beg := beg; select.end := pos; select.time := Oberon.Time();
  998.             Viewers.Broadcast(select)
  999.         END
  1000.     END ShiftBlock;
  1001.     PROCEDURE Write (F: Frame; ch: CHAR; fnt: Fonts.Font; col, voff: SHORTINT);
  1002.         VAR loc: Location; parc: Parc; org, pos, pbeg: LONGINT; i: INTEGER;
  1003.             buf: ARRAY 32 OF CHAR;
  1004.             copy: Texts.CopyMsg; input: Oberon.InputMsg;
  1005.         PROCEDURE Visible(ch: CHAR): BOOLEAN;
  1006.             VAR pat: Display.Pattern; dx, x, y, w, h: INTEGER;
  1007.         BEGIN Display.GetChar(W.fnt.raster, ch, dx, x, y, w, h, pat); RETURN dx > 0
  1008.         END Visible;
  1009.         PROCEDURE InsertBuffer;
  1010.             VAR i, j: INTEGER; ch: CHAR;
  1011.         BEGIN i := 0; j := 0; ch := buf[i];
  1012.             WHILE ch # 0X DO
  1013.                 IF (ch = TAB) OR (ch = CR) OR (ch = " ") OR Visible(ch) THEN Texts.Write(W, ch); INC(j) END;
  1014.                 INC(i); ch := buf[i]
  1015.             END; 
  1016.             IF j > 0 THEN Texts.Insert(F.text, pos, W.buf); INC(pos, LONG(j)) END
  1017.         END InsertBuffer;
  1018.         PROCEDURE Flush;
  1019.             VAR ch: CHAR;
  1020.         BEGIN
  1021.             WHILE Input.Available() > 0 DO Input.Read(ch) END
  1022.         END Flush;
  1023.     BEGIN
  1024.         IF F.hasSel & (ch = CRSL) THEN ShiftBlock(F, -1)
  1025.         ELSIF F.hasSel & (ch = CRSR) THEN ShiftBlock(F, 1)
  1026.         ELSIF F.hasCar THEN pos := F.carloc.pos;
  1027.             IF ch = DEL THEN
  1028.                 IF pos > F.org THEN DEC(pos); Texts.Delete(F.text, pos, pos + 1); Flush END
  1029.             ELSIF (ch = DELRIGHT) & (pos < F.text.len) THEN Texts.Delete(F.text, pos, pos + 1); Flush        (*<< mah del right *)
  1030.             ELSIF ch = HOME THEN pos := Pos (F, F.X, F.carloc.y)                (*<< mah beg of line *)
  1031.             ELSIF ch = EOL THEN pos := Pos (F, F.X+F.W-1, F.carloc.y)        (*<< mah end of line *)
  1032.             ELSIF (ch = CRSL) & (pos > 0) THEN DEC(pos)
  1033.             ELSIF (ch = CRSR) & (pos < F.text.len) THEN INC(pos)
  1034.             ELSIF (ch = CRSU) & (pos > 0) THEN                                    (*<< mah cursor up *)
  1035.                 org:=Pos (F, F.carloc.x+1, F.carloc.y+F.carloc.line.h);
  1036.                 IF org=pos THEN Show (F, F.org-1) END;
  1037.                 pos:=Pos (F, F.carloc.x+1, F.carloc.y+F.carloc.line.h)
  1038.             ELSIF (ch = CRSD) & (pos < F.text.len) THEN                        (*<< mah cursor down *)
  1039.                 org:=Pos (F, F.carloc.x+1, F.carloc.y-F.carloc.line.next.h);
  1040.                 IF (org=pos) & (F.trailer.org+F.trailer.len#F.text.len) THEN Show (F, F.trailer.next.next.org) END;
  1041.                 LocatePos (F, pos, loc);
  1042.                 pos:=Pos (F, F.carloc.x+1, loc.y-loc.line.next.h)
  1043.             ELSIF ch=PGUP THEN                                                    (*<< mah page up *)
  1044.                 LocateLine (F, F.Y+F.H-1, loc); i:=loc.y-F.Y-F.bot;
  1045.                 Back (F, i, pos); Show (F, pos); pos:=F.org
  1046.             ELSIF ch=PGDN THEN                                                (*<< mah page down *)
  1047.                 IF F.trailer.org+F.trailer.len = F.text.len THEN pos:=F.trailer.org
  1048.                 ELSE LocateLine (F, F.Y, loc); Show (F, loc.org); pos:=F.org
  1049.                 END
  1050.             ELSIF (ch = CRSL) OR (ch = CRSU) OR (ch = CRSD) OR (ch = CRSR) OR (ch = PGUP) OR (ch=PGDN) THEN 
  1051.             ELSIF (ch = BRK) OR (ch = ShiftBRK) THEN
  1052.                 ParcBefore(F.text, pos, P, pbeg); P.handle(P, copy); parc := copy.e(Parc);
  1053.                 IF ch = BRK THEN EXCL(parc.opts, pageBreak) ELSE INCL(parc.opts, pageBreak) END;
  1054.                 PickAttributes(W, F.text, pos);
  1055.                 Texts.WriteElem(W, parc); Texts.Insert(F.text, pos, W.buf); INC(pos)
  1056.             ELSIF (ch = TAB) OR (ch = CR) OR (ch >= " ") THEN
  1057.                 PickAttributes(W, F.text, pos);
  1058.                 IF ch = CR THEN buf[0] := CR; i := 1; org := F.carloc.org; BegOfLine(F.text, org, FALSE);
  1059.                     Texts.OpenReader(R, F.text, org);
  1060.                     REPEAT Texts.Read(R, ch) UNTIL (R.elem = NIL) OR ~(R.elem IS Parc);
  1061.                     WHILE (Texts.Pos(R) <= pos) & (ch <= " ") & (ch # Texts.ElemChar) & (i < 31) DO
  1062.                         buf[i] := ch; INC(i); Texts.Read(R, ch)
  1063.                     END
  1064.                 ELSIF ch = LF THEN buf[0] := CR; i:=1            (*<< mah Enter on numeric pad has no autoindent *)
  1065.                 ELSE buf[0] := ch; i := 1
  1066.                 END;
  1067.                 buf[i] := 0X; InsertBuffer
  1068.             END;
  1069.             IF pos < F.org THEN Show(F, F.org - 1) END;
  1070.             SetCaret(F, pos);
  1071.             WHILE F.carloc.y < F.Y + F.bot DO Show(F, F.trailer.next.next.org); Flush; SetCaret(F, pos) END
  1072.         ELSIF F.focus # NIL THEN input.id := Oberon.consume; input.ch := ch;
  1073.             input.fnt := fnt; input.col := col; input.voff := voff; F.focus.handle(F.focus, input)
  1074.         END
  1075.     END Write;
  1076.     PROCEDURE TouchElem (F: Frame; VAR x, y: INTEGER; VAR keysum: SET);
  1077.         VAR loc: Location; e: Texts.Elem; pbeg: LONGINT; y0: INTEGER;
  1078.             track: TrackMsg;
  1079.     BEGIN
  1080.         LocateChar(F, x, y, loc); e := R.elem;
  1081.         IF (e # NIL) & (loc.x + e.W DIV Unit <= F.X + F.W - F.right) THEN
  1082.             ParcBefore(F.text, loc.pos, P, pbeg); y0 := loc.y + loc.line.dsr - SHORT(P.dsr DIV Unit) + loc.dy;
  1083.             IF (loc.x <= x) & (x < loc.x + e.W DIV Unit) & (keysum= {middleKey}) THEN
  1084.                 track.X := x; track.Y := y; track.keys := keysum;
  1085.                 track.fnt := R.fnt; track.col := R.col; track.pos := Texts.Pos(R) - 1;
  1086.                 track.frame := F; track.X0 := loc.x; track.Y0 := y0;
  1087.                 e.handle(e, track); keysum := {}
  1088.             END
  1089.         END
  1090.     END TouchElem;
  1091.     PROCEDURE Edit (F: Frame; x, y: INTEGER; keysum: SET);
  1092.         VAR ef: Display.Frame; text: Texts.Text; beg, end, time, pos: LONGINT; keys: SET; ch: CHAR;
  1093.             loc: Location; delta, res: INTEGER; copyover: Oberon.CopyOverMsg; input: Oberon.InputMsg;
  1094.     BEGIN
  1095.         IF x < F.X + F.barW THEN pos := F.org;    (* scroll bar *)
  1096.             IF leftKey IN keysum THEN TrackLine(F, x, y, pos, keysum)
  1097.             ELSIF rightKey IN keysum THEN TrackLine(F, x, y, pos, keysum); LocateLine(F, y, loc);
  1098.                 pos := F.org; delta := loc.y - (F.Y + F.bot); Back(F, delta, pos)
  1099.             ELSIF middleKey IN keysum THEN
  1100.                 REPEAT TrackMouse(x, y, keys, keysum) UNTIL keys = {};
  1101.                 IF keysum = {middleKey, leftKey} THEN pos := F.text.len; (*BegOfLine(F.text, pos, TRUE);*)
  1102.                     Back(F, F.H - F.bot - F.top - 30 (*heuristic*), pos);
  1103.                 ELSIF keysum = {middleKey, rightKey} THEN pos := 0
  1104.                 ELSIF (F.Y <= y) & (y <= F.Y + F.H) THEN pos := CoordToPos(F, y - F.Y); BegOfLine(F.text, pos, TRUE)
  1105.                 END
  1106.             ELSE DrawCursor(x, y); keysum := cancel
  1107.             END;
  1108.             IF keysum # cancel THEN ShowFrom(F, pos) END
  1109.         ELSE    (* text area *)
  1110.             ef := ThisSubFrame(F, x, y);
  1111.             IF ef # NIL THEN    (* within sub-frame *)
  1112.                 IF (F.focus # ef) & (keysum = {leftKey}) THEN
  1113.                     REPEAT TrackMouse(x, y, keys, keysum) UNTIL keys = {};
  1114.                     IF keysum = {leftKey} THEN RemoveSelection(F); RemoveCaret(F); PassSubFocus(F, ef); RETURN END
  1115.                 ELSIF F.focus = ef THEN input.id := Oberon.track; input.keys := keysum; input.X := x; input.Y := y;
  1116.                     ef.handle(ef, input); RETURN
  1117.                 END
  1118.             END;
  1119.             IF keysum # {} THEN TouchElem(F, x, y, keysum);
  1120.                 IF keysum = {} THEN RETURN END
  1121.             END;
  1122.             IF leftKey IN keysum THEN Oberon.PassFocus(Viewers.This(F.X, F.Y)); TrackCaret(F, x, y, keysum);
  1123.                 IF (keysum = {leftKey, middleKey}) & F.hasCar THEN Oberon.GetSelection(text, beg, end, time);
  1124.                     IF time >= 0 THEN Texts.Save(text, beg, end, B);
  1125.                         Texts.Insert(F.text, F.carloc.pos, B); SetCaret(F, F.carloc.pos + (end - beg))
  1126.                     END
  1127.                 ELSIF (keysum = {leftKey, rightKey}) & F.hasCar & (F.carloc.pos < F.text.len) THEN
  1128.                     Oberon.GetSelection(text, beg, end, time);
  1129.                     IF time >= 0 THEN Texts.OpenReader(R, F.text, F.carloc.pos); Texts.Read(R, ch);
  1130.                         Texts.ChangeLooks(text, beg, end, {0, 1, 2}, R.fnt, R.col, R.voff)
  1131.                     END
  1132.                 END
  1133.             ELSIF middleKey IN keysum THEN TrackWord(F, x, y, pos, keysum);
  1134.                 IF keysum # cancel THEN
  1135.                     IF rightKey IN keysum THEN
  1136.                         par.vwr := Viewers.This(F.X, F.Y);
  1137.                         par.frame := F; par.text := F.text; par.pos := pos;
  1138.                         Oberon.Call("Edit.Open", par, FALSE, res)
  1139.                     ELSE
  1140.                         Call(F, pos, keysum = {middleKey, leftKey})
  1141.                     END
  1142.                 END
  1143.             ELSIF rightKey IN keysum THEN TrackSelection(F, x, y, keysum);
  1144.                 IF (keysum = {rightKey, middleKey}) & F.hasSel THEN
  1145.                     copyover.text := F.text; copyover.beg := F.selbeg.pos; copyover.end := F.selend.pos;
  1146.                     Oberon.FocusViewer.handle(Oberon.FocusViewer, copyover)
  1147.                 ELSIF (keysum = {rightKey, leftKey}) & F.hasSel THEN Oberon.PassFocus(Viewers.This(F.X, F.Y));
  1148.                     Texts.Delete(F.text, F.selbeg.pos, F.selend.pos); SetCaret(F, F.selbeg.pos)
  1149.                 END
  1150.             ELSE DrawCursor(x, y)
  1151.             END
  1152.         END
  1153.     END Edit;
  1154.     (** General **)
  1155.     PROCEDURE Copy (SF, DF: Frame);
  1156.     BEGIN
  1157.         DF.handle := SF.handle; DF.text := SF.text; DF.org := SF.org;
  1158.         DF.col := SF.col; DF.left := SF.left; DF.right := SF.right; DF.top := SF.top; DF.bot := SF.bot;
  1159.         DF.barW := SF.barW; DF.hasCar := FALSE; DF.hasSel := FALSE; DF.showsParcs := SF.showsParcs;
  1160.         DF.focus := NIL; DF.trailer := NIL
  1161.     END Copy;
  1162.     PROCEDURE Handle* (f: Display.Frame; VAR msg: Display.FrameMsg);
  1163.         VAR F, F1: Frame; pos: LONGINT;
  1164.     BEGIN F := f(Frame);
  1165.         IF msg IS Oberon.InputMsg THEN
  1166.             WITH msg: Oberon.InputMsg DO
  1167.                 IF msg.id = Oberon.consume THEN Write(F, msg.ch, msg.fnt, msg.col, msg.voff)
  1168.                 ELSIF msg.id = Oberon.track THEN Edit(F, msg.X, msg.Y, msg.keys)
  1169.                 END
  1170.             END
  1171.         ELSIF msg IS Oberon.ControlMsg THEN
  1172.             WITH msg: Oberon.ControlMsg DO
  1173.                 IF msg.id = Oberon.defocus THEN RemoveCaret(F)
  1174.                 ELSIF msg.id = Oberon.neutralize THEN
  1175.                     RemoveCaret(F); RemoveSelection(F); PassSubFocus(F, NIL); NotifySubFrames(F, msg)
  1176.                 ELSE NotifySubFrames(F, msg)
  1177.                 END
  1178.             END
  1179.         ELSIF msg IS Oberon.CopyMsg THEN
  1180.             WITH msg: Oberon.CopyMsg DO
  1181.                 IF msg.F = NIL THEN NEW(F1); msg.F := F1 END;
  1182.                 Copy(F, msg.F(Frame))
  1183.             END
  1184.         ELSIF msg IS UpdateMsg THEN NotifySubFrames(F, msg);
  1185.             WITH msg: UpdateMsg DO
  1186.                 IF msg.text = F.text THEN Update(F, msg) END
  1187.             END
  1188.         ELSIF msg IS InsertElemMsg THEN
  1189.             IF F.hasCar THEN pos := F.carloc.pos;
  1190.                 PickAttributes(W, F.text, pos);
  1191.                 Texts.WriteElem(W, msg(InsertElemMsg).e);
  1192.                 Texts.Insert(F.text, pos, W.buf);
  1193.                 SetCaret(F, pos + 1)
  1194.             END
  1195.         ELSIF msg IS Oberon.SelectionMsg THEN NotifySubFrames(F, msg);
  1196.             WITH msg: Oberon.SelectionMsg DO
  1197.                 IF F.hasSel & (F.time > msg.time) THEN
  1198.                     msg.text := F.text; msg.beg := F.selbeg.pos; msg.end := F.selend.pos; msg.time := F.time
  1199.                 END
  1200.             END
  1201.         ELSIF msg IS Oberon.CopyOverMsg THEN NotifySubFrames(F, msg);
  1202.             WITH msg: Oberon.CopyOverMsg DO
  1203.                 IF F.hasCar THEN Texts.Save(msg.text, msg.beg, msg.end, B);
  1204.                     Texts.Insert(F.text, F.carloc.pos, B); SetCaret(F, F.carloc.pos + (msg.end - msg.beg))
  1205.                 END
  1206.             END
  1207.         ELSIF msg IS MenuViewers.ModifyMsg THEN
  1208.             WITH msg: MenuViewers.ModifyMsg DO
  1209.                 F.handle(F, neutralize); Resize(F, F.X, msg.Y, F.W, msg.H)
  1210.             END
  1211.         ELSIF msg IS SelectMsg THEN NotifySubFrames(F, msg);
  1212.             WITH msg: SelectMsg DO
  1213.                 IF (msg.text = F.text) & ~F.hasSel THEN Oberon.RemoveMarks(F.X, F.Y, F.W, F.H);
  1214.                     F.handle(F, neutralize);
  1215.                     SetSelection(F, msg.beg, msg.end); F.time := msg.time;
  1216.                     IF F.hasSel THEN F.selbeg.pos := msg.beg; F.selend.pos := msg.end END
  1217.                 END
  1218.             END
  1219.         ELSE NotifySubFrames(F, msg)
  1220.         END
  1221.     END Handle;
  1222.     PROCEDURE Open* (F: Frame; T: Texts.Text; pos: LONGINT);
  1223.     BEGIN
  1224.         F.handle := Handle; F.text := T; F.org := pos; F.col := Display.black;
  1225.         F.left := left; F.right := right; F.top := top; F.bot := bot;
  1226.         F.barW := barW; F.hasCar := FALSE; F.hasSel := FALSE; F.showsParcs := FALSE; F.trailer := NIL
  1227.     END Open;
  1228.     PROCEDURE NotifyDisplay* (T: Texts.Text; op: INTEGER; beg, end: LONGINT);
  1229.         VAR msg: UpdateMsg;
  1230.     BEGIN
  1231.         msg.text := T; msg.id := op; msg.beg := beg; msg.end := end; Viewers.Broadcast(msg)
  1232.     END NotifyDisplay;
  1233.     PROCEDURE Text* (name: ARRAY OF CHAR): Texts.Text;
  1234.         VAR text: Texts.Text;
  1235.     BEGIN
  1236.         NEW(text); Texts.Open(text, name); text.notify := NotifyDisplay; RETURN text
  1237.     END Text;
  1238.     PROCEDURE NewText* (T: Texts.Text; pos: LONGINT): Frame;
  1239.         VAR frame: Frame;
  1240.     BEGIN
  1241.         NEW(frame); Open(frame, T, pos);
  1242.         RETURN frame
  1243.     END NewText;
  1244.     PROCEDURE NewMenu* (name, commands: ARRAY OF CHAR): Frame;
  1245.         VAR T, T1: Texts.Text; buf: Texts.Buffer; frame: Frame; fn: ARRAY 32 OF CHAR; i: INTEGER;
  1246.     BEGIN
  1247.         T := Text("");
  1248.         Texts.WriteString(W0, name); Texts.WriteString(W0, " | "); Texts.Append(T, W0.buf);
  1249.         IF commands[0] = "^" THEN
  1250.             i := 0; REPEAT INC(i); fn[i-1] := commands[i] UNTIL commands[i] = 0X;
  1251.             IF Files.Old(fn) = NIL THEN
  1252.                 Texts.WriteString(W0, "System.Close  System.Grow  System.Copy Edit.Store "); Texts.Append(T, W0.buf)
  1253.             ELSE
  1254.                 NEW(T1); Texts.Open(T1, fn);
  1255.                 NEW(buf); Texts.OpenBuf(buf); Texts.Save(T1, 0, T1.len, buf); Texts.Append(T, buf)
  1256.             END
  1257.         ELSE
  1258.             Texts.WriteString(W0, commands); Texts.Append(T, W0.buf)
  1259.         END;
  1260.         NEW(frame); Open(frame, T, 0);
  1261.         frame.col := Display.white; frame.left := 6; frame.top := 0; frame.bot := 0; frame.barW := 0;
  1262.         RETURN frame
  1263.     END NewMenu;
  1264. BEGIN
  1265.     Texts.OpenWriter(W); Texts.OpenWriter(W0);
  1266.     Texts.SetFont(W0, Fonts.Default); Texts.SetColor(W0, Display.white); Texts.SetOffset(W0, 0);
  1267.     neutralize.id := Oberon.neutralize;
  1268.     NEW(par);
  1269.     NEW(B); Texts.OpenBuf(B);
  1270.     menuH := Fonts.Default.height + 2;
  1271.     barW := menuH; left := barW + 6; right := 8; top := 6; bot := 6;
  1272.     InitDefParc
  1273. END TextFrames.
  1274.